package pattern.tempate;

public class JDBCTemplateDemo {
    public static void main(String[] args){
        Update update = new Update();
        update.service();
    }
}

//模板方法模式基于一个抽象类，将核心的变动代码交给用户实现，自己实现公共的代码
abstract class JDBCTemlate {

    private void openConnection() {
        System.out.println("开启连接");
    }

    private void closeConnection() {
        System.out.println("关闭连接");
    }

    private void startTransacation() {
        System.out.println("开启事务");
    }

    private void commitTransacation() {
        System.out.println("提交事务");
    }

    private void rollbackTransacation() {
        System.out.println("回滚事务");
    }

    public abstract void doService();

    public void service() {
        openConnection();
        startTransacation();
        try {
            doService();//交给子类实现
            commitTransacation();
        } catch (Exception e) {
            rollbackTransacation();
        } finally {
            closeConnection();
        }
    }
}

class Update extends JDBCTemlate {
    @Override
    public void doService() {
        System.out.println("update user set useranme = 'zs' and password='123'");
    }
}